﻿$(document).ready(function(){
	makeGetBlockRequest();
	appendBlockLinks();
	//addRanks();
});
function makeRequest(data){
	chrome.runtime.sendMessage(data, function(response) {
		for(var i =0; i < response.blocked_users.length;i++){
			var toHide = $('dl dt [href="user.php?i='+response.blocked_users[i]+'"]').parent().parent();
			toHide.html('<dl><dt>Tiltott felhasználó</dt><dd><center><b>blokkolt tartalom</b> <a id="'+response.blocked_users[i]+'" class="unblocklink" href="javascript:void(0)">unblock</a></center></dd><dl>');
			toHide.find('a').click(function(){
				makeUnBlockRequest($(this).attr('id'));
			});
		};
	});
}
function makeGetBlockRequest(){
	userURL = $("ul.barL a:first").attr('href');
	userId = userURL.substring(11,userURL.length);
	data = {type:0,act_user:userId};
	makeRequest(data);
}
function makeBlockRequest(sender){
	userURL = $("ul.barL a:first").attr('href');
	userId = userURL.substring(11,userURL.length);
	blockUserURL=sender.parent().parent().parent().parent().find('dt a').attr('href');
	blockUserId = blockUserURL.substring(11,blockUserURL.length);
	data = {type:1,act_user:userId,blocked_user:blockUserId};
	makeRequest(data);
}
function makeUnBlockRequest(unblockId){
	userURL = $("ul.barL a:first").attr('href');
	userId = userURL.substring(11,userURL.length);
	data = {type:2,act_user:userId,unblock_user:unblockId};
	makeRequest(data);
	window.location.reload();
}
function appendBlockLinks(){
	$('div.fR.links').append('<a class="blocklink" href="javascript:void(0)">block!</a>');
	$('a.blocklink').click(function(){
		userURL = $("ul.barL a:first").attr('href');
		userId = userURL.substring(11,userURL.length);
		makeBlockRequest($(this));
	});
}
function addRanks(){
	var posts = $("dl");
	// posts.each(function(element){
		// var content = $(this).find("td.desc.bc1").html();
		// var splitat = content.indexOf("</a><br>");
		// var rangstart = content.search('[0-9][0-9]*[,]?[0-9]*[ ]*<a href="pm');
		// var rangend = content.search('<a href="pm');
		// var postcount = parseInt(content.substring(rangstart,rangend).replace(',',''));
		// // rang kiszamitasa
		// if(postcount==0)
			// rang = '-';
		// else if(postcount <= 100)
			// rang = 'Multi';
		// else if(postcount <= 500)
			// rang = 'Newbie';
		// else if(postcount <= 1000)
			// rang = 'Trainee';
		// else if(postcount <= 2000)
			// rang = 'Beginner';
		// else if(postcount <= 5000)
			// rang = 'Veteran';
		// else if(postcount <= 10000)
			// rang = 'Post hunter';
		// else if(postcount <= 20000)
			// rang = 'Spamer';
		// else if(postcount <= 40000)
			// rang = 'Nolifer';
		// else
			// rang = 'Almost beni';
		
		// if($(this).find("dt a").attr('href').substr(11,6) == '266692')
			// rang = 'Emperor';
		// // rang hozzafuzese
		// $(this).find("td.desc.bc1").append('Rank: ' + rang);
	// });
}